package Interceptor;

import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.validate.Validator;

/**
 * Created by Properly on 2018/7/2.
 */
public class LoginFormValidator extends Validator {
    private String errorKey = "error";

    @Override
    protected void validate(Controller controller) {
        //用户名不为空
        if (controller.getPara("username").isEmpty()) {
            addError(errorKey, "用户名为空");
        }
        if (controller.getPara("password").isEmpty()) {
            addError(errorKey, "密码为空");
        }
       validateCaptcha("captcha", errorKey, "验证码不正确");


    }

    @Override
    protected void handleError(Controller controller) {
        String message = controller.getAttr(errorKey);
        Kv result = Kv.by("success", false).set("message", message);
        controller.renderJson(result);

    }
}
